package com.wangsir.servlet;

import com.wangsir.entity.Student;
import com.wangsir.service.StudentService;
import com.wangsir.util.ApiResult;
import com.wangsir.util.MyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

@WebServlet("/student-personal")
public class StudentPersonal  extends HttpServlet {
    private StudentService studentService = new StudentService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收请求的编码格式
        request.setCharacterEncoding("utf-8");
        //返回数据的编码格式
        response.setContentType("application/json;charset=utf-8");
        boolean hasPermission = MyUtil.hasPermission(request, response, true, "student");
        if(!hasPermission){
            return;
        }
        String sno = request.getParameter("sno");
        Student loginStudent = (Student)request.getSession().getAttribute("user");
        if(!loginStudent.getSno().equals(sno)){
            response.getWriter().write(ApiResult.json(false,"只能修改个人的信息！拒绝访问"));
        }
        String r = request.getParameter("r");
        if("edit".equals(r)){
            //修改个人的信息
            Student student = new Student();
            student.setSno(request.getParameter("sno"));
            student.setName(request.getParameter("name"));
            student.setPassword(request.getParameter("password"));
            student.setTele(request.getParameter("tele"));
            student.setEnterDate(MyUtil.strToDate(request.getParameter("enterDate")));
            student.setGender(request.getParameter("gender"));
            //年龄必须赋值，否则会出现空字符串
            if(request.getParameter("age") != null ){
                student.setAge(Integer.parseInt(request.getParameter("age")));
            }else{
                student.setAge(0);
            }
            String msg = studentService.updateStudent(student);
            if(msg != null){
                response.getWriter().write(ApiResult.json(false,msg));
                return;
            }else{
                request.getSession().setAttribute("user",studentService.getStudentBySno(sno));
                response.getWriter().write(ApiResult.json(true,"修改个人信息成功！"));
                return;
            }
        }
        if("pwd".equals(r)){
            String oldPwd = request.getParameter("oldPwd");
            String newPwd = request.getParameter("newPwd");
            String newPwd2 = request.getParameter("newPwd2");
           if(!newPwd.equals(newPwd2)){
               response.getWriter().write(ApiResult.json(false,"两次密码输入不一致！"));
               return;
           }
            if(!oldPwd.equals(((Student) request.getSession().getAttribute("user")).getPassword())){
                response.getWriter().write(ApiResult.json(false,"旧密码输入错误！"));
                return;
            }
            Student student = new Student();
            student.setSno(sno);
            student.setPassword(newPwd);
            studentService.updateStudent(student);
            loginStudent.setPassword(newPwd);
            request.getSession().setAttribute("user",loginStudent);
            response.getWriter().write(ApiResult.json(true,"修改成功！"));
        }
        //
    }

}
